package com.xinghou.platform.dao.main.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.xinghou.platform.dao.bases.JdbcTemplate;
import com.xinghou.platform.dao.bases.database.CustomerContextHolder;
import com.xinghou.platform.dao.main.AlbumImageDAO;
import com.xinghou.platform.model.main.AlbumImage;

/**
 * 
 * @author wzk
 *
 */
@Repository
public class AlbumImageDAOImpl implements AlbumImageDAO {
	@Resource
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public AlbumImage getAlbumImageById(Integer Id) throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql = "SELECT Id, User_id,Parent_id,TYPE,Uri,STATUS,Praise_count,Create_time FROM album_image WHERE Id=?";
		AlbumImage albumImage = jdbcTemplate.queryForObject(sql,new Object[] { Id }, new AlbumImageMapper());
		CustomerContextHolder.clearCustomerType();
		return albumImage;
	}

	@Override
	public int updateAlbumImage(AlbumImage albumImage) throws DataAccessException {
		String strSQL = "UPDATE album_image SET Id=?,User_id=?,Parent_id=?,TYPE=?,Uri=?,STATUS=?,Praise_count=?,Create_time=? WHERE Id=?";
		Object[] params = { albumImage.getId(),albumImage.getUserId(),albumImage.getParentId(),albumImage.getType(),albumImage.getUri(),
							albumImage.getStatus(),albumImage.getPraiseCount(),albumImage.getCreateTime(),albumImage.getId()};
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_WRITE);
		int ret = jdbcTemplate.update(strSQL.toString(), params);
		CustomerContextHolder.clearCustomerType();
		return ret;
	}

	@Override
	public int saveAlbumImage(AlbumImage albumImage) throws DataAccessException {
		String strSQL = "INSERT INTO album_image (Id,User_id,Parent_id,TYPE,Uri,STATUS,Praise_count,Create_time) VALUES (?,?,?,?,?,?,?,?)";
		Object[] params = {albumImage.getId(),albumImage.getUserId(),albumImage.getParentId(),albumImage.getType(),albumImage.getUri(),
							albumImage.getStatus(),albumImage.getPraiseCount(),albumImage.getCreateTime()};
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_WRITE);
		int ret = jdbcTemplate.save(strSQL.toString(), params);
		CustomerContextHolder.clearCustomerType();
		return ret;
	}

	@Override
	public List<AlbumImage> getAlbumImageList(int userId, Timestamp time,int rowNum) throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql = "SELECT Id, User_id,Parent_id,TYPE,Uri,STATUS,Praise_count,Create_time FROM album_image WHERE User_id=? AND Create_time<? ORDER BY Create_time DESC LIMIT ?";
		List<AlbumImage> list = jdbcTemplate.query(sql,new Object[] { userId,time,rowNum }, new AlbumImageMapper());
		CustomerContextHolder.clearCustomerType();
		return list;
	}
	
	@SuppressWarnings("unused")
	private static final class AlbumImageMapper implements RowMapper<AlbumImage> {
		public AlbumImage mapRow(ResultSet rs, int rowNum) throws SQLException {
			AlbumImage albumImage = new AlbumImage();
			albumImage.setId(rs.getInt("Id"));
			albumImage.setUserId(rs.getInt("User_id"));
			albumImage.setParentId(rs.getInt("Parent_id"));
			albumImage.setType(rs.getByte("type"));
			albumImage.setUri(rs.getString("Uri"));
			albumImage.setStatus(rs.getByte("STATUS"));
			albumImage.setPraiseCount(rs.getInt("Praise_count"));
			albumImage.setCreateTime(rs.getDate("Create_time"));
			return albumImage;
		}
	}

}
